package org.locker.service.impl;

import org.locker.common.core.utils.MapstructUtils;
import org.locker.common.core.utils.StringUtils;
import org.locker.common.mybatis.core.page.TableDataInfo;
import org.locker.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.locker.domain.bo.BalanceRecordBo;
import org.locker.domain.vo.BalanceRecordVo;
import org.locker.domain.BalanceRecord;
import org.locker.mapper.BalanceRecordMapper;
import org.locker.service.IBalanceRecordService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户余额操作记录Service业务层处理
 *
 * @author winnie
 * @date 2023-10-11
 */
@RequiredArgsConstructor
@Service
public class BalanceRecordServiceImpl  extends ServiceImpl<BalanceRecordMapper,BalanceRecord>  implements IBalanceRecordService {

    private final BalanceRecordMapper baseMapper;

    /**
     * 查询用户余额操作记录
     */
    @Override
    public BalanceRecordVo queryById(Long balanceRecordId){
        return baseMapper.selectVoById(balanceRecordId);
    }

    /**
     * 查询用户余额操作记录列表
     */
    @Override
    public TableDataInfo<BalanceRecordVo> queryPageList(BalanceRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BalanceRecord> lqw = buildQueryWrapper(bo);
        Page<BalanceRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户余额操作记录列表
     */
    @Override
    public List<BalanceRecordVo> queryList(BalanceRecordBo bo) {
        LambdaQueryWrapper<BalanceRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BalanceRecord> buildQueryWrapper(BalanceRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BalanceRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, BalanceRecord::getUserId, bo.getUserId());
        lqw.eq(bo.getPositionId() != null, BalanceRecord::getPositionId, bo.getPositionId());
        lqw.eq(bo.getWithdrawId() != null, BalanceRecord::getWithdrawId, bo.getWithdrawId());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderNo()), BalanceRecord::getOrderNo, bo.getOrderNo());
        lqw.between(params.get("beginAmount") != null && params.get("endAmount") != null,
            BalanceRecord::getAmount ,params.get("beginAmount"), params.get("endAmount"));
        lqw.eq(StringUtils.isNotBlank(bo.getRecordType()), BalanceRecord::getRecordType, bo.getRecordType());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), BalanceRecord::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增用户余额操作记录
     */
    @Override
    public Boolean insertByBo(BalanceRecordBo bo) {
        BalanceRecord add = MapstructUtils.convert(bo, BalanceRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setBalanceRecordId(add.getBalanceRecordId());
        }
        return flag;
    }

    /**
     * 修改用户余额操作记录
     */
    @Override
    public Boolean updateByBo(BalanceRecordBo bo) {
        BalanceRecord update = MapstructUtils.convert(bo, BalanceRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BalanceRecord entity){
        //做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户余额操作记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
